package com.fingard.dsp.bank.directbank.epay01;

import com.fingard.dsp.bank.directbank.DirectBase;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class EPAY01Base extends DirectBase {
    protected String getCharset() {
        return getBankConfig().getCharset("UTF-8");
    }

    public String sendDataToBank(String reqData) throws IOException {
        RequestConfig requestConfig = RequestConfig // 请求设置
                .custom().setConnectionRequestTimeout(3000) // 从连接池获取连接超时时间
                .setConnectTimeout(3000) // 连接超时时间
                .setSocketTimeout(3000).build(); // 套接字超时时间
        HttpClientBuilder builder = HttpClients.custom().setDefaultRequestConfig(requestConfig).setMaxConnTotal(5); // 设置最大连接数
        CloseableHttpClient httpClient = builder.build();

        CloseableHttpResponse response = null;
        WriteBankLogLn2("发送银行服务地址:" + bankFront.serverURL);
        HttpPost procPost = new HttpPost(bankFront.serverURL);
        WriteBankLogStep2(reqData);
        StringEntity entity = new StringEntity(reqData, ContentType.create("application/json", getCharset()));
        procPost.setEntity(entity);
        InputStreamReader is = null;
        BufferedReader reader = null;
        try {
            response = httpClient.execute(procPost);
            is = new InputStreamReader(response.getEntity().getContent(), getCharset());
            reader = new BufferedReader(is);
            String tempBf = null;
            StringBuffer html = new StringBuffer();
            while ((tempBf = reader.readLine()) != null) {
                html.append(tempBf);
            }
            WriteBankLogStep3(html.toString());
            return html.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                if (procPost != null) {
                    procPost.releaseConnection();
                }
                if (is != null) {
                    is.close();
                }
                if (reader != null) {
                    reader.close();
                }
                if (httpClient != null) {
                    try {
                        httpClient.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    }

}